/**
 * 上传页
 */
package com.xiaomuwu.android.ui.upload

import android.os.Bundle
import android.text.Editable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import com.xiaomuwu.android.MainActivity
import com.xiaomuwu.android.R
import com.xiaomuwu.android.XiaoMuWuApplication
import com.xiaomuwu.android.logic.Repository
import com.xiaomuwu.android.logic.model.ForModify
import com.xiaomuwu.android.logic.model.ForUpload
import com.xiaomuwu.android.ui.Displayer
import com.xiaomuwu.android.ui.ForSearch
import com.xiaomuwu.android.ui.LoginStatus
import kotlinx.android.synthetic.main.upload_res_fragment.*
import kotlinx.android.synthetic.main.user_detail_fragment.*

class UploadResFragment:Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.upload_res_fragment,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 根据页面设置控件显示
        if(Displayer.pageStatus=="ModifyRes") {
            upload?.text="修改"   // 设置按钮文字

            // 若为 “修改资料” 页面，则自动填充资料信息
            resNameEdit?.setText(Displayer.resName) // 设置资料名编辑框填充文字
            tagsEdit?.setText(Displayer.resTags)    // 设置标签编辑框填充文字
            resLinkEdit?.setText(Displayer.resLink) // 设置资料链接编辑框填充文字
            aboutEdit?.setText(Displayer.resAbout)  // 设置简介编辑框填充文字
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

//        var resName:String?=null
        var tags:String?=null   // 标签
//        var resLink:String?=null
        var about:String?=null  // 资料简介

//        resNameEdit.addTextChangedListener{editable->
//            if(editable?.isNotBlank() == true) {
//                resName = editable.toString()
//            }
//        }
//        tagsEdit.addTextChangedListener { editable->
//            if(editable?.isNotBlank()==true){
//                tags=editable.toString()
//            }
//        }
//        resLinkEdit.addTextChangedListener { editable->
//            if(editable?.isNotBlank()==true){
//                resLink=editable.toString()
//            }
//        }
//        aboutEdit.addTextChangedListener { editable->
//            if(editable?.isNotBlank()==true){
//                about=editable.toString()
//            }
//        }

//        when(Displayer.pageStatus){
//            "UploadRes"->{
//                upload?.setOnClickListener {
//                    if (resName==null){
//                        Toast.makeText(XiaoMuWuApplication.context,"资料名不能为空！",Toast.LENGTH_SHORT).show()
//                    }else{
//                        if(resLink==null){
//                            Toast.makeText(XiaoMuWuApplication.context,"资料链接不能为空！",Toast.LENGTH_SHORT).show()
//                        }else{
//                            Repository.uploadRes(ForUpload(resName!!,tags,resLink!!,LoginStatus.user_ID!!,about))
//                            Toast.makeText(XiaoMuWuApplication.context,"上传成功",Toast.LENGTH_SHORT).show()
//                        }
//                    }
//                }
//            }
//            "Modify"->{
//                upload?.setOnClickListener {
//                    if (resName==null){
//                        Toast.makeText(XiaoMuWuApplication.context,"资料名不能为空！",Toast.LENGTH_SHORT).show()
//                    }else{
//                        if(resLink==null){
//                            Toast.makeText(XiaoMuWuApplication.context,"资料链接不能为空！",Toast.LENGTH_SHORT).show()
//                        }else{
//                            Repository.modifyRes(ForModify(ForSearch.resID,resName!!,tags,resLink!!,LoginStatus.user_ID!!,about))
//                            Toast.makeText(XiaoMuWuApplication.context,"修改成功",Toast.LENGTH_SHORT).show()
//                        }
//                    }
//                }
//            }
//        }

        // 根据页面设置按钮点击监听
        when(Displayer.pageStatus) {
            "UploadRes" -> {
                upload?.setOnClickListener {
                    if(tagsEdit.text.isNotBlank()){
                        tags=tagsEdit.text.toString()   // 设置标签
                    }
                    if(aboutEdit.text.isNotBlank()){
                        about=aboutEdit.text.toString() // 设置简介
                    }
                    if (resNameEdit.text.isBlank()){
                        Toast.makeText(XiaoMuWuApplication.context,"资料名不能为空！",
                            Toast.LENGTH_SHORT).show()
                    }else{
                        if(resLinkEdit.text.isBlank()) {
                            Toast.makeText(XiaoMuWuApplication.context,"资料链接不能为空！",
                                Toast.LENGTH_SHORT).show()
                        }else {
                            try {
                                // 上传资料
                                Repository.uploadRes(
                                    ForUpload(
                                        resNameEdit.text.toString(), tags,
                                        resLinkEdit.text.toString(), LoginStatus.user_ID!!, about
                                    )
                                )
                                Toast.makeText(
                                    XiaoMuWuApplication.context, "上传成功",
                                    Toast.LENGTH_SHORT
                                ).show()
                            }catch (e: Exception) {
                                e.printStackTrace()
                                Log.d("UploadResFragment","上传失败")
                            }
                        }
                    }
                }
            }
            "ModifyRes" -> {
                upload?.setOnClickListener {
                    if(tagsEdit.text.isNotBlank()){
                        tags=tagsEdit.text.toString()   // 设置标签
                    }
                    if(aboutEdit.text.isNotBlank()){
                        about=aboutEdit.text.toString() // 设置简介
                    }
                    if (resNameEdit.text.isBlank()){
                        Toast.makeText(
                            XiaoMuWuApplication.context,"资料名不能为空！", Toast.LENGTH_SHORT
                        ).show()
                    }else{
                        if(resLinkEdit.text.isBlank()) {
                            Toast.makeText(
                                XiaoMuWuApplication.context,"资料链接不能为空！",
                                Toast.LENGTH_SHORT
                            ).show()
                        }else {
                            try {
                                // 修改资料
                                Repository.modifyRes(
                                    ForModify(
                                        ForSearch.resID, resNameEdit.text.toString(), tags,
                                        resLinkEdit.text.toString(), LoginStatus.user_ID!!, about
                                    )
                                )
                                Toast.makeText(
                                    XiaoMuWuApplication.context, "修改成功",
                                    Toast.LENGTH_SHORT
                                ).show()
                            }catch (e: Exception) {
                                e.printStackTrace()
                                Log.d("UploadResFragment","修改失败")
                            }
                        }
                    }
                }
            }
        }
    }

}